<?php
namespace app\lawcase\validate;

use think\Validate;
use think\facade\Db;

class CourtCheck extends Validate
{
    // 自定义验证规则
	protected function checkOne($value,$rule,$data=[])
	{
		$id = isset($data['id'])?$data['id']:0;
		$count = Db::name('Court')->where([['name','=',$data['name']],['id','<>',$id],['delete_time','=',0]])->count();
		return $count == 0 ? true : false;
	}

    protected $rule = [
        'name' => 'require|max:100|checkOne',
    ];

    protected $message = [
        'name.require' => '法院名称不能为空',
        'name.checkOne' => '法院名称已经存在',
        'name.max' => '法院名称最多100个字符',
        'level.require' => '请选择法院级别',
        'level.in' => '法院级别选择错误',
        'provinceid.require' => '请选择省份',
        'provinceid.number' => '省份选择错误',
        'cityid.require' => '请选择城市',
        'cityid.number' => '城市选择错误',
        'address.max' => '详细地址最多255个字符', // 移除了地址必填的错误提示
        'id.require' => '缺少更新条件',
    ];

    protected $scene = [
        'add' => ['name', 'level'],
        // 显式添加 id 字段到 edit 场景
        'edit' => ['id', 'name'],
    ];

}